local fengrang = fk.CreateSkill{
  name = "hanqing__fengrang",
}

Fk:loadTranslationTable{
  ["hanqing__fengrang"] = "封壤",
  [":hanqing__fengrang"] = "出牌阶段结束时，或当你受到伤害后，你可以将至多X张中央区的牌固定。回合结束时，你可以令至多X名角色依次选择一项："..
  "1.弃置一张中央区没有花色的牌；2.你视为对其使用一张冰【杀】（X为你已损失体力值）。",

  ["@@hanqing__fengrang"] = "被固定",
  ["#hanqing__fengrang-invoke"] = "封壤：是否要固定中央区的牌？",
  ["#hanqing__fengrang-choose"] = "封壤：你可以选择至多%arg名角色，令其选择弃牌或视为你对其使用冰【杀】",
  ["#hanqing__fengrang-card"] = "封壤：你可以固定中央区至多%arg张牌",
  ["#hanqing__fengrang-discard"] = "封壤：请弃置一张中央区没有的花色的牌，或点“取消”视为 %src 对你使用冰【杀】",
}

local spec = {
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = fengrang.name,
      prompt = "#hanqing__fengrang-invoke",
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room:getBanner("@$CenterArea"), function (id)
      return Fk:getCardById(id):getMark("@@hanqing__fengrang") == 0
    end)
    local ids = room:askToChooseCards(player, {
      target = player,
      min = 1,
      max = player:getLostHp(),
      flag = { card_data = {{ "$CenterArea", cards }} },
      skill_name = fengrang.name,
      prompt = "#hanqing__fengrang-card:::"..player:getLostHp(),
    })
    for _, id in ipairs(ids) do
      room:addCardMark(Fk:getCardById(id), MarkEnum.IncludeCenterCards, 1)
      room:setCardMark(Fk:getCardById(id), "@@hanqing__fengrang", 1)
    end
  end,
}

fengrang:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fengrang.name) and player.phase == Player.Play and
      player:isWounded() and
      table.find(player.room:getBanner("@$CenterArea"), function (id)
        return Fk:getCardById(id):getMark("@@hanqing__fengrang") == 0
      end)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

fengrang:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fengrang.name) and
      player:isWounded() and
      table.find(player.room:getBanner("@$CenterArea"), function (id)
        return Fk:getCardById(id):getMark("@@hanqing__fengrang") == 0
      end)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

fengrang:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fengrang.name) and player:isWounded()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = player:getLostHp(),
      targets = room.alive_players,
      skill_name = fengrang.name,
      prompt = "#hanqing__fengrang-choose:::"..player:getLostHp(),
      cancelable = true,
    })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(event:getCostData(self).tos) do
      if not p.dead then
        local cards = table.filter(p:getCardIds("he"), function (c)
          return not p:prohibitDiscard(c) and Fk:getCardById(c).suit ~= Card.NoSuit and
            not table.find(room:getBanner("@$CenterArea"), function (c2)
              return Fk:getCardById(c):compareSuitWith(Fk:getCardById(c2))
            end)
        end)
        if #room:askToDiscard(p, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = fengrang.name,
            pattern = tostring(Exppattern{ id = cards }),
            prompt = "#hanqing__fengrang-discard:"..player.id,
            cancelable = true,
          }) == 0 and
          p ~= player then
          room:useVirtualCard("ice__slash", nil, player, p, fengrang.name, true)
        end
      end
    end
  end,
})

fengrang:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player.seat == 1 then
      for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@hanqing__fengrang") > 0 and
            not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId):getMark("@@hanqing__fengrang") > 0 and
          not table.contains(room.discard_pile, info.cardId) then
          room:removeCardMark(Fk:getCardById(info.cardId), MarkEnum.IncludeCenterCards, 1)
          room:setCardMark(Fk:getCardById(info.cardId), "@@hanqing__fengrang", 0)
        end
      end
    end
  end,
})

fengrang:addEffect(fk.AfterDrawPileShuffle, {
  can_refresh = function (self, event, target, player, data)
    if player.seat == 1 then
      return table.find(Fk.cards, function (card)
        return card:getMark("@@hanqing__fengrang") > 0 and
          not table.contains(player.room.discard_pile, card.id)
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, card in ipairs(Fk.cards) do
      if not table.contains(room.discard_pile, card.id) then
        room:removeCardMark(card, MarkEnum.IncludeCenterCards, 1)
        room:setCardMark(card, "@@hanqing__fengrang", 0)
      end
    end
  end,
})

fengrang:addEffect(fk.BeforeTurnStart, {
  can_refresh = function (self, event, target, player, data)
    if player.seat == 1 then
      return table.find(Fk.cards, function (card)
        return card:getMark("@@hanqing__fengrang") > 0
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local banner = {}
    for _, card in ipairs(Fk.cards) do
      if card:getMark("@@hanqing__fengrang") > 0 then
        if table.contains(room.discard_pile, card.id) then
          table.insert(banner, card.id)
        else
          room:removeCardMark(card, MarkEnum.IncludeCenterCards, 1)
          room:setCardMark(card, "@@hanqing__fengrang", 0)
        end
      end
    end
    room:setBanner("@$CenterArea", banner)
  end,
})

fengrang:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return fengrang
